---------------------------------------------------------------------------
-- 3DS MAX9.0 MaxScript Tutorial
-- Scripted by MrShaderKr
--
-- Thanks to:
-- - MaxScript Reference Manual
--
-- License:
-- - You can freely distribute it and modify it.
-- - If you find any bugs, email me to fix it.
--
-- Version history:
--
-- - (2010/04/15/Thr) - 
-- - (2009/11/25/Wed) - refactoring , separation
--
-- Known Bugs:
-- --


fn myEcho msg =
(
    format "%\n" (msg) to:listener
)

fn myLog msg =
(
    format "%\n" (msg) to:listener
)

fn myDoesFileExist filename =
(
    local temp = fopen filename "rb"
    if temp != undefined then
    (
        fclose temp
        true
    )
    else
    (
        false
    )
)

fn myFindFile path filename recurse:false =
(
	local check = path + "\\" + filename
	if doesFileExist check then return check
	if recurse then
	(
		local dirs = getDirectories ( path + "/*" )
		for dir in dirs do
		(
			local ret = myFindFile dir filename recurse:true
			if ret != undefined then return ret
		)
	)
	
	return undefined
)


fn myGetFileSize bstream =
(
    offset = ftell bstream
    fseek bstream 0 #seek_end
    size = ftell bstream
    fseek bstream offset #seek_set
    
    return size
)

fn myEOF bstream =
(
    local curr = ftell bstream
    if curr == undefined then return false
    fseek bstream 0 #seek_end
    local end = ftell bstream
    fseek bstream curr #seek_set
    
    if end - curr > 0 then
    (
        return false
    )
    else
    (
        return true
    )
)

fn mySkipBytes bstream count =
(
   offset = ftell bstream
   offset += count
   fseek bstream offset #seek_set
)
fn mySkipBytesW bstream count =
(
   offset = ftell bstream
   offset += count * 2
   fseek bstream offset #seek_set
)

fn myReadFixedString bstream fixedLen =
(
    local str = ""
    for i = 1 to fixedLen do
    (
        local ch = readByte bstream #unsigned
        str += bit.intAsChar ch
        if ch == 0 then
        (
            -- Ignore junk
            mySkipBytes bstream ( fixedLen - i )
            exit
        )
    )
    str
)
fn myReadFixedStringW bstream fixedLen =
(
    local str = ""
    for i = 1 to fixedLen do
    (
        local ch = readShort bstream #unsigned
        str += bit.intAsChar ch
        if ch == 0 then
        (
            -- Ignore junk
            mySkipBytesW bstream ( fixedLen - i )
            exit
        )
    )
    str
)

fn myReadFourCC bstream =
(
    local iter          = 0 as integer
    local fourCC        = "" as string

    for iter = 1 to 4 do
    (
        fourCC          += bit.intAsChar ( readByte bstream #unsigned )
    )

    return fourCC
)

fn myHalfAsFloat input16 = 
(
	inputAsInt  = input16
	sign        = bit.get inputAsInt 16
	exponent    = ( bit.shift ( bit.and inputAsInt ( bit.hexasint "7C00" ) ) -10 ) as integer - 16
	mantissa    = bit.and inputAsInt ( bit.hexasint "03FF" )
	if sign == true then sign = 1 else sign = 0
	exponentF   = exponent + 127
	--Ouput 32 bit integer representing a 32 bit float
	outputAsFloat = bit.or ( bit.or ( bit.shift mantissa 13 ) ( bit.shift exponentF 23 ) ) ( bit.shift sign 31 )
	--Output Check	
	return bit.intasfloat outputasfloat
)


fn myHalfAsFloat2 i16 =
(
    value       = i16
    
    sign        = bit.get value 16
    exponent    = bit.and ( bit.shift value -10  ) ( bit.hexAsInt "0000001F" )
    mantissa    = bit.and value ( bit.hexAsInt "03FF" )
    
    if sign == true then sign = 1 else sign = 0
    
    if ( exponent == 0 ) then
    (
        if ( mantissa == 0 ) then
        (
            return bit.intAsFloat ( bit.shift sign 31 )   
        )
        else
        (
            while ( 0 != ( bit.and mantissa ( bit.hexAsInt "00000400" ) ) ) do
            (
                mantissa = bit.shift mantissa 1
                exponent = exponent - 1
            )
            
            exponent = exponent + 1
            manissa = bit.and mantissa ( bit.not ( bit.hexAsInt "00000400" ) )   
        )   
    )
    else if ( exponent == 31 ) then
    (
        if ( mantissa == 0 ) then
        (
            return bit.intAsFloat ( bit.or ( bit.shift sign 31 ) ( bit.hexAsInt "7F800000" ) )   
        )
        else
        (
            return bit.intAsFloat ( bit.or ( bit.or ( bit.shift sign 31 ) ( bit.hexAsInt "7F800000" ) ) ( bit.shift mantissa 13 ) ) 
        )   
    )
    
    exponent = exponent + ( 127 - 15 )
    mantissa = bit.shift mantissa 13 
    
    
    outputInt = bit.or ( bit.or mantissa ( bit.shift exponent 23 ) ) ( bit.shift sign 31 )
    
    return bit.intAsFloat outputInt
)

